package com.luanye.enjoy.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luanye.enjoy.entity.Activity;
import com.luanye.enjoy.entity.Tribe;
import com.luanye.enjoy.entity.TribeUser;
import com.luanye.enjoy.entity.User;
import com.luanye.enjoy.enums.ResultCode;
import com.luanye.enjoy.mapper.ActivityMapper;
import com.luanye.enjoy.mapper.TribeMapper;
import com.luanye.enjoy.mapper.TribeUserMapper;
import com.luanye.enjoy.mapper.UserMapper;
import com.luanye.enjoy.service.ActivityService;
import com.luanye.enjoy.service.TribeService;
import com.luanye.enjoy.service.TribeUserService;
import com.luanye.enjoy.service.UserService;
import com.luanye.enjoy.utils.BeanCopyUtils;
import com.luanye.enjoy.vo.*;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service("tribeService")
public class TribeServiceImpl extends ServiceImpl<TribeMapper, Tribe> implements TribeService {
    @Autowired
    private TribeUserMapper tribeUserMapper;
    @Autowired
    private TribeUserService tribeUserService;

    @Autowired
    private UserService userService;

    @Autowired
    private TribeMapper tribeMapper;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public RespBean createTribe(Tribe tribe, String openid) {
        tribe.setCreateId(openid);
        save(tribe);
        LambdaQueryWrapper<Tribe> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tribe::getBrief,tribe.getBrief());
        queryWrapper.eq(Tribe::getTrideName,tribe.getTrideName());
        queryWrapper.eq(Tribe::getLocation,tribe.getLocation());

        Tribe one = getOne(queryWrapper);
        //生成用户关联
        TribeUser tribeUser = new TribeUser();
        tribeUser.setTribeId(one.getId());
        tribeUser.setOpenId(openid);
        tribeUserMapper.insert(tribeUser);
        return RespBean.success();
    }

    @Override
    public RespBean getAllByNum() {
        LambdaQueryWrapper<Tribe> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Tribe::getJoinNum);
        Page<Tribe> page = new Page<>(1,5);

        page(page,queryWrapper);

        List<Tribe> records = page.getRecords();
        List<TribeVo> tribeVos = BeanCopyUtils.copyBeanList(records, TribeVo.class);

        return RespBean.success(tribeVos);
    }

    @Override
    public RespBean getDetail(Integer id, String openid) {
        Tribe byId = getById(id);

        LambdaQueryWrapper<TribeUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TribeUser::getOpenId,openid);
        queryWrapper.eq(TribeUser::getTribeId,id);
        TribeUser tribeUser = tribeUserMapper.selectOne(queryWrapper);

        List<TribeUser> numByTribeId = tribeUserService.getNumByTribeId(id);

        List<UserVo> userVos = new ArrayList<>();

        for (TribeUser user : numByTribeId){
            User userByOpenId = userMapper.getUserByOpenId(user.getOpenId());
            UserVo userVo = BeanCopyUtils.copyBean(userByOpenId, UserVo.class);
            userVos.add(userVo);
        }

        List<ActivityVo> byTribeId = activityService.getByTribeId(id);


        TribeDetailVo tribeDetailVo = BeanCopyUtils.copyBean(byId, TribeDetailVo.class);
        tribeDetailVo.setIsTakePartIn(Objects.nonNull(tribeUser));
        tribeDetailVo.setUserVos(userVos);
        tribeDetailVo.setActivities(byTribeId);
        tribeDetailVo.setIsCreate(byId.getCreateId().equals(openid));

        return RespBean.success(tribeDetailVo);
    }

    @Override
    public RespBean joinTribe(Integer id, String openid) {
        //刷新joinNum
        Tribe byId = getById(id);
        byId.setJoinNum(byId.getJoinNum()+1);
        updateById(byId);
        //写入关系表
        TribeUser tribeUser = new TribeUser();
        tribeUser.setTribeId(id);
        tribeUser.setOpenId(openid);
        tribeUserMapper.insert(tribeUser);
        return RespBean.success();
    }

    @Override
    public RespBean exitTribe(Integer id, String openid) {
        Tribe byId = getById(id);
        byId.setJoinNum(byId.getJoinNum()-1);
        updateById(byId);
        LambdaQueryWrapper<TribeUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TribeUser::getTribeId,id);
        queryWrapper.eq(TribeUser::getOpenId,openid);

        tribeUserMapper.delete(queryWrapper);
        return RespBean.success();
    }

    @Override
    public RespBean getAllMember(Integer id) {
        LambdaQueryWrapper<TribeUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TribeUser::getTribeId,id);

        List<TribeUser> tribeUsers = tribeUserMapper.selectList(queryWrapper);
        List<UserVo> userVos = new ArrayList<>();
        for (TribeUser user : tribeUsers){
            User userByOpenId = userMapper.getUserByOpenId(user.getOpenId());
            UserVo userVo = BeanCopyUtils.copyBean(userByOpenId, UserVo.class);
            userVos.add(userVo);
        }
        return RespBean.success(userVos);
    }

    @Override
    public RespBean getMy(String openid) {
        LambdaQueryWrapper<TribeUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TribeUser::getOpenId,openid);
        List<TribeUser> tribeUsers = tribeUserMapper.selectList(queryWrapper);
        List<TribeVo> tribes = new ArrayList<>();
        for (TribeUser user : tribeUsers){
            Tribe byId = getById(user.getTribeId());
            TribeVo tribeVo = BeanCopyUtils.copyBean(byId, TribeVo.class);
            tribes.add(tribeVo);
        }
        return RespBean.success(tribes);
    }

    @Override
    public RespBean dissolve(Integer id, String openid) {
        //查询部落是否存在
        Tribe tribe = getById(id);
        if (Objects.isNull(tribe)) return RespBean.error(ResultCode.TRIBE_NOT_EXIST);
        //判断是否为创建人
        if (!tribe.getCreateId().equals(openid)) return RespBean.error(ResultCode.NOT_CREATEOR);
        //删除部落列
        LambdaQueryWrapper<Tribe> delete = new LambdaQueryWrapper<>();
        delete.eq(Tribe::getCreateId,openid);
        delete.eq(Tribe::getId,id);
        tribeMapper.delete(delete);
        //删除部落成员
        LambdaQueryWrapper<TribeUser> deleteAll = new LambdaQueryWrapper<>();
        deleteAll.eq(TribeUser::getTribeId,id);
        tribeUserMapper.delete(deleteAll);
        //活动 部落列置空
        UpdateWrapper<Activity> update = new UpdateWrapper<>();
        update.eq("tride_id",id);
        update.set("tride_id",null);
        activityMapper.update(null,update);
        return RespBean.success();
    }

    @Override
    @DS("slave")
    public RespBean getByUser(String openid) {
        LambdaQueryWrapper<Tribe> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tribe::getCreateId,openid);

        List<Tribe> tribes = tribeMapper.selectList(queryWrapper);
        List<TribeVo> tribeVos = BeanCopyUtils.copyBeanList(tribes, TribeVo.class);
        return RespBean.success(tribeVos);
    }
}
